package jp.gr.java_conf.ryz.summarize.android.activity;

import java.util.List;

import jp.gr.java_conf.ryz.summarize.android.R;
import jp.gr.java_conf.ryz.summarize.android.logic.DataCreator;
import jp.gr.java_conf.ryz.summarize.android.model.Category;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class SelectCategoryActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_category);
		
		init();
	}
	
	
	private void init()
	{
		ScrollView scrollView = (ScrollView)findViewById(R.id.activity_select_category_root);
		
		final TableLayout tableLayout = new TableLayout(SelectCategoryActivity.this);
		scrollView.addView(tableLayout);
		
		new AsyncTask<String, Integer, List<Category>>()
		{

			@Override
			protected List<Category> doInBackground(String... params)
			{
				List<Category> categories = DataCreator.getCategories();
				return categories;
			}

			@Override
			protected void onPostExecute(List<Category> result)
			{
				int size = result.size();
				TableRow row = null;
				for (int i = 0; i < size; i++)
				{
					final Category eachCategory = result.get(i);
					
					if ((i % 2) == 0)
					{
						row = new TableRow(SelectCategoryActivity.this);
						tableLayout.addView(row);
					}
					
					Button button = new Button(SelectCategoryActivity.this);
//					button.setLayoutParams(new TableLayout.LayoutParams(100, LayoutParams.MATCH_PARENT));
					button.setText(eachCategory.getName());
					button.setOnClickListener(new OnClickListener()
					{
						@Override
						public void onClick(View view)
						{
							Intent intent = new Intent(SelectCategoryActivity.this, SelectBoardActivity.class);
							intent.putExtra(SelectBoardActivity.PARAM_CATEGORY, eachCategory);
							startActivity(intent);
						}
					});
					row.addView(button);
				}
			}
			
		}.execute();
	}

}
